#!/bin/bash
# This shell script used to install the vim configure files
# Author: Guo Wenxue<guowenxue@gmail.com>
# Usage: 
#   I. Create an install bin file
#       [guowenxue@centos6 ~]$ tar -czf vim_conf.tar.gz .vim .vimrc 
#       [guowenxue@centos6 ~]$ cat install.sh vim_conf.tar.gz > vim_conf_Vx.x.x.bin 
#
#  II. Install the configure files:
#       [guowenxue@centos6 ~]$ ./vim_conf.bin 
#       Please input your C file head default Author Name:  Guo Wenxue
#       Please input your C file head default Email Address:  guowenxue@gmail.com
#       Please input your C file head default company Name:  <U can skip the company>
#       Install vim configure files to /home/guowenxue
#

VIM_BAK_FILE=vim_conf_bak.tar.gz

if [ -d ~/.vim -o -f ~/.vimrc ] ; then
    echo "Backup original configure files in $VIM_BAK_FILE"
    tar -czf $VIM_BAK_FILE .vim .vimrc 2>/dev/null
    rm -rf ~/.vim ~/.vimrc
fi

echo -n "Please input your C file head default Author Name:  "
read author

echo -n "Please input your C file head default Email Address:  "
read email

echo -n "Please input your C file head default company Name:  "
read company

if [ ! -n "$author" -o ! -n "$email" ] ; then
    echo "ERROR: Must give default author name and email."
    exit -1;
fi

# Get the compress packet from the file and decompress it to $HOME
echo "Install vim configure files to $HOME"
sed -n '1,/^exit 0$/!p' $0 | tar -xz -C ~

# Use sed command to modify the Auther information
file1=~/.vim/c-support/templates/Templates
sed -i -e "s/^|AUTHOR|.*/|AUTHOR|    = $author/g" $file1
sed -i -e "s/^|EMAIL|.*/|EMAIL|     = $email/g"    $file1

# Use sed command to modify the Copyright information
file2=~/.vim/c-support/templates/c.comments.template
if [ -n "$company" ] ; then 
    sed -i -e "s/Copyright.*/Copyright:  (C) |YEAR| $company/g" $file2
else
    sed -i -e "s/Copyright.*/Copyright:  (C) |YEAR| $author<$email>/g" $file2
fi

#WARNNING: The last line must start and end by "exit 0" without any other character follwed!
exit 0
